var path = require('path')
var build = require('../lib/build.js')
var download = require('../lib/download.js').download
var config = require('../lib/config.js')
var rm = require('rimraf')

var downloadPath = config.downloadPath

module.exports = {
  downloadAndBuild: function (repoKey, dest) {
    var src = path.join(downloadPath, repoKey)
    return download(repoKey, src)
      .then(function () {
        return build(src, dest)
      })
      .catch(function (err) {
        console.log(err)
      })
  },

  rmTmpDir: function () {
    return new Promise(function (resolve, reject) {
      rm(downloadPath, function (err) {
        if (err) {
          return reject(err)
        }
        resolve()
      })
    })
      .catch(function (err) {
        console.log(err)
      })
  },

  noop: function () {
    return false
  },

  wait: function (interval) {
    var _interval = interval || 0
    return new Promise(function (resolve, reject) {
      setTimeout(function () {
        resolve()
      }, _interval)
    })
  }
}
